﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscountSharing.LocalModels;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;

namespace DiscountSharing.LocalControls
{
    public partial class DiscountElement : UserControl
    {
        public Post CurrentPost { private get; set; }

        public DiscountElement()
        {
            InitializeComponent();

            WriterImage.Tap += new EventHandler<GestureEventArgs>(WriterImage_Tap);
            InfoGrid.Tap += new EventHandler<GestureEventArgs>(InfoGrid_Tap);
            Loaded += (o, e) =>
                {
                    InitializeControls();
                };
        }

        private void InitializeControls()
        {
            WriterImage.Source = new BitmapImage(new Uri(CurrentPost.Writer.SmallPic));
            Writer.Text = CurrentPost.Writer.Name;
            Time.Text = CurrentPost.UpdatedTime.ToString("mm:ss MMM dd");
            TypeImage.Source = new BitmapImage(new Uri(String.Format("/DiscountSharing;component/Images/Category/{0}.png", CurrentPost.DiscountType.ToString().ToLower()), UriKind.Relative));
            Place.Text = CurrentPost.Place;
            Place.Text = CurrentPost.Place;
            Message.Text = CurrentPost.Message;
            PicIndicatorImage.Source = new BitmapImage(new Uri("/DiscountSharing;component/Images/Indicator/picture.png", UriKind.Relative));
            LocationIndicatorImage.Source = new BitmapImage(new Uri("/DiscountSharing;component/Images/Indicator/location.png", UriKind.Relative));
            Statistics.Text = String.Format("L:{0}, C:{1}", CurrentPost.LikeCount, CurrentPost.CommentCount);
        }

        private void InfoGrid_Tap(object sender, GestureEventArgs e)
        {
            Grid currentGrid = (Grid)sender;
        }

        private void WriterImage_Tap(object sender, GestureEventArgs e)
        {
            Image writerImage = (Image)sender;
            DiscountElement element = (DiscountElement)writerImage.DataContext;
            ((PhoneApplicationFrame)Application.Current.RootVisual).Navigate(new Uri(String.Format("/Discounts.xaml?show=by_friend&id={0}", element.CurrentPost.Writer.ID), UriKind.Relative));
        }
    }
}
